package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping(path = "/role")
@CrossOrigin
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 查询下角色信息
     * @return
     */
    @GetMapping(path = "/all")
    public Result findAll() {
        try {
            List<Role> roles = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 添加角色 菜单
     * @param role
     * @param permissionIds
     * @param menuIds
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 分页查询检查项数据
     *
     * @param queryPageBean
     * @return
     */
    @GetMapping(path = "/page")
    public Result findPage(QueryPageBean queryPageBean) {
        //分页查询检查组信息
        try {
            PageResult pageResult = roleService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 根据id删除角色
     *
     * @param id
     * @return
     */
    @DeleteMapping(path = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        try {
            //删除检查项
            roleService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 根据id查询数据详情
     *
     * @param id
     * @return
     */
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable int id) {
        try {
            //查询检查项
            Role role= roleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 确定编辑, 更新数据
     *
     * @param role
     * @return
     */
    @PutMapping
    public Result edit(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds) {
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @GetMapping(path = "/{roleId}/menu")
    public Result selectedMenuId(@PathVariable("roleId") Integer roleId) {
        try {
            Integer[] menusId = roleService.selectedMenusId(roleId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS,menusId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @GetMapping(path = "/{roleId}/permission")
    public Result selectedPermission(@PathVariable("roleId") Integer roleId) {
        try {
            Integer[] permissionsId = roleService.selectedPermissionsId(roleId);
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS,permissionsId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }
}
